﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using WinRTXamlToolkit.Controls.Extensions;
using System.Collections.ObjectModel;
// 使用者控制項項目範本已記錄在 http://go.microsoft.com/fwlink/?LinkId=234236

namespace Centny
{
    public delegate void PrepareItemContainer(GridViewExt gview, Windows.UI.Xaml.DependencyObject element, object item);
    public partial class GroupGridView : UserControl
    {
        public DataTemplate ItemTemplate
        {
            get
            {
                return this._ZoomOutGridView.ItemTemplate;
                //return null;
            }
            set
            {
                this._ZoomOutGridView.ItemTemplate = value;
            }
        }
        public DataTemplateSelector ItemTemplateSelector
        {
            get
            {
                return this._ZoomOutGridView.ItemTemplateSelector;
                //return null;
            }
            set
            {
                this._ZoomOutGridView.ItemTemplateSelector = value;
            }
        }
        public ItemsPanelTemplate ItemsPanel
        {
            get
            {
                return this.ZoomOutGridView.ItemsPanel;
            }
            set
            {
                this.ZoomOutGridView.ItemsPanel = value;
            }
        }
        public String ItemsPath
        {
            get
            {
                return this._GroupItemSource.ItemsPath.Path;
            }
            set
            {
                this._GroupItemSource.ItemsPath = new PropertyPath(value);
            }
        }
        public GridView ZoomInGridView
        {
            get
            {
                return this._ZoomInGridView;
            }
        }
        public GridView ZoomOutGridView
        {
            get
            {
                return this._ZoomOutGridView;
            }
        }
        public GroupStyle ZoomInGroupStyle
        {
            get
            {
                return this._ZoomInGridView.GroupStyle.First();
            }
            set
            {
                this._ZoomInGridView.GroupStyle.Add(value);
            }
        }
        public DataTemplate GroupHeaderTemplate
        {
            get
            {
                return this.ZoomInGroupStyle.HeaderTemplate;
            }
            set
            {
                this.ZoomInGroupStyle.HeaderTemplate = value;
            }
        }
        public DataTemplateSelector GroupHeaderTemplateSelector
        {
            get
            {
                return this.ZoomInGroupStyle.HeaderTemplateSelector;
            }
            set
            {
                this.ZoomInGroupStyle.HeaderTemplateSelector = value;
            }
        }
        public DataTemplate GroupItemTemplate
        {

            get
            {
                return this._ZoomInGridView.ItemTemplate;
            }
            set
            {
                this._ZoomInGridView.ItemTemplate = value;
            }
        }
        public DataTemplateSelector GroupTemplateSelector
        {
            get
            {
                return this._ZoomInGridView.ItemTemplateSelector;
            }
            set
            {
                this._ZoomInGridView.ItemTemplateSelector = value;
            }
        }
        public ItemsPanelTemplate GroupStylePanel
        {
            get
            {
                return this.ZoomInGroupStyle.Panel;
            }
            set
            {
                this.ZoomInGroupStyle.Panel = value;
            }
        }
        public ItemsPanelTemplate GroupItemsPanel
        {
            get
            {
                return this.ZoomInGridView.ItemsPanel;
            }
            set
            {
                this.ZoomInGridView.ItemsPanel = value;
            }
        }
        public event ItemClickEventHandler ItemClick
        {
            add
            {
                this.ZoomInGridView.ItemClick += value;
            }
            remove
            {
                this.ZoomInGridView.ItemClick -= value;
            }
        }
        public Object GroupItems
        {
            get
            {
                return this._GroupItemSource.Source;
            }
            set
            {
                this._GroupItemSource.Source = value;
                this._NormalItemSource.Source = value;
            }
        }
        public PrepareItemContainer OnPrepareItemContainer
        {
            get
            {
                return this._ZoomInGridView.OnPrepareItemContainer;
            }
            set
            {
                this._ZoomInGridView.OnPrepareItemContainer = value;
            }
        }
        public bool HaveOverStyle
        {
            get
            {
                return this._ZoomInGridView.HaveOverStyle;
            }
            set
            {
                this._ZoomInGridView.HaveOverStyle = value;
            }
        }
        public GroupGridView()
        {
            this.InitializeComponent();
            //this._ZoomInGridView.SizeChanged += SizeChangedEventHandler;
            //this._ZoomInGridView.Loaded += OnLoaded;
        }
        //public void SizeChangedEventHandler(object sender, SizeChangedEventArgs e)
        //{
        //    ScrollViewer sv = VisualTreeHelperExtensions.GetFirstDescendantOfType<ScrollViewer>(this._ZoomInGridView);

        //}
        //public void OnLoaded(object sender, RoutedEventArgs e)
        //{
        //    GridView gv = sender as GridView;
        //    ScrollViewer sv = VisualTreeHelperExtensions.GetFirstDescendantOfType<ScrollViewer>(this._ZoomInGridView);
        //}
        protected virtual void SZoomViewChangeCompleted(object sender, SemanticZoomViewChangedEventArgs e)
        {
            if (!e.IsSourceZoomedInView)
            {
                this._ZoomInGridView.ScrollIntoView(e.SourceItem.Item);
            }
        }
        protected virtual void SZoomtViewStarted(object sender, SemanticZoomViewChangedEventArgs e)
        {
        }
        public event VectorChangedEventHandler<Object> VectorChanged
        {
            add
            {
                this._NormalItemSource.View.VectorChanged += value;
            }
            remove
            {
                this._NormalItemSource.View.VectorChanged -= value;
            }
        }
    }
    public class GridViewExt : GridView
    {
        public PrepareItemContainer OnPrepareItemContainer { get; set; }
        public bool HaveOverStyle { get; set; }
        public GridViewExt()
        {
            this.HaveOverStyle = true;
        }
        public void BasePrepareContainerForItem(Windows.UI.Xaml.DependencyObject element, object item)
        {
            base.PrepareContainerForItemOverride(element, item);
        }
        protected override void PrepareContainerForItemOverride(Windows.UI.Xaml.DependencyObject element, object item)
        {
            if (this.OnPrepareItemContainer == null)
            {
                base.PrepareContainerForItemOverride(element, item);
            }
            else
            {
                this.OnPrepareItemContainer(this, element, item);
                FrameworkElement ui = element as FrameworkElement;
                if (!this.HaveOverStyle)
                {
                    ui.Style = this.FindName("NoOverGridViewItemStyle") as Style;
                }
            }
        }
    }
}
